#!/usr/bin/python3
# -* - coding: UTF-8 -* -

'''
操作
'''

from selenium.common.exceptions import ElementClickInterceptedException
import os
import sys

from selenium.webdriver import ActionChains

curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

from utils import SetResult, Support, LogSys


class ElementOperate():
    WAIT_TIME = 0.5

    def __init__(self, driver):
        self.driver = driver

    def click(self, el):
        try:
            LogSys.logInfo('click element')
            el.click()
        except ElementClickInterceptedException:
            SetResult.set_fail('catch err :ElementClickInterceptedException')
        finally:
            Support.sleep(self.WAIT_TIME)

    def input(self, el, input_text):
        LogSys.logInfo('input :{} '.format(input_text))
        try:
            el.click()
            el.clear()
            el.send_keys(input_text)
        except ElementClickInterceptedException:
            SetResult.set_fail('catch err :ElementClickInterceptedException')
        except ElementClickInterceptedException:
            SetResult.set_fail('catch err :ElementClickInterceptedException')
        finally:
            Support.sleep(self.WAIT_TIME)

    def click_point(self, el):
        rec = el.rect
        x = rec['x'] + rec['width'] / 2
        y = rec['y'] + rec['height'] / 2
        ActionChains(self.driver).move_by_offset(x, y).click().perform()
